//
//  StarPageViewController.m
//  iOS_NongLian
//
//  Created by n369 on 15/12/14.
//  Copyright © 2015年 NongLian. All rights reserved.
//

#define kBorder 30
#define Button_W 200.0f
#define Button_H 50.0f

#import "StarPageViewController.h"
#import "AppDelegate.h"
#import "TabBarViewController.h"

@interface StarPageViewController () <UIScrollViewDelegate> {
    UIPageControl * _pgConl;
}

@property (nonatomic, strong) UIScrollView * startPage;
@property (nonatomic, strong) NSMutableArray * dataArray;

@end

@implementation StarPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupStartPage];
}

//创建引导页
- (void)setupStartPage
{
    self.startPage = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    
    self.startPage.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.startPage];
    [self createBackView];
    [self fillData];
    [self createPageControl];
}

- (void)createPageControl {
    _pgConl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, ScreenHeight - ScreenWidth / 3 - 40, ScreenWidth, 20)];
    [_pgConl setValue:[UIImage imageNamed:@"blueCol"] forKey:@"_currentPageImage"];
    [_pgConl setValue:[UIImage imageNamed:@"whiteCol"] forKey:@"_pageImage"];
    _pgConl.numberOfPages = 3;
    _pgConl.currentPage = 0;
    [self.view addSubview:_pgConl];
}

- (void)createBackView {
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 20)];
    view.backgroundColor = RGBA(0, 0, 0, .2);
    [self.view addSubview:view];
}

//设置引导页图片
- (void)fillData
{
    _dataArray = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < 3; i++) {
        
        NSString * imageName = [NSString stringWithFormat:@"png%i.png", i];
        [_dataArray addObject:imageName];
    }
    for (int i = 0; i < _dataArray.count; i++) {
        
        UIImageView * imageV = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.view.frame.size.width, 0, self.startPage.frame.size.width, self.startPage.frame.size.height)];
        imageV.image = [UIImage imageNamed:self.dataArray[i]];
        imageV.userInteractionEnabled = YES;
        
        // 在最后一页添加立即体验按钮
        if (_dataArray.count - 1 == i) {
            
            CGFloat border = 0.0f;
            if (iPhone4S) {
                border = 35.0f;
            }else if (iPhone5S) {
                border = 45.0f;
            }else if (iPhone6S){
                border = 55.0f;
            }else {
                border = 60.0f;
            }
            
            UIButton * inBtn = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width - Button_W)/2.0, ScreenHeight - ScreenWidth / 3, Button_W, Button_H)];
            [inBtn setTitle:@"马上进入" forState:UIControlStateNormal];
            inBtn.backgroundColor = RGBA(0, 0, 0, .6);
            [inBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            inBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
            inBtn.titleLabel.font = TextFontWithSize(20);
            inBtn.layer.masksToBounds = YES;
            inBtn.layer.cornerRadius = 5.0f;
            inBtn.layer.borderColor  = RGBA(255, 107, 41, 1).CGColor;
            inBtn.layer.borderWidth  = 1;
            //            inBtn.layer.
            [inBtn addTarget:self action:@selector(enterButtonAction) forControlEvents:UIControlEventTouchUpInside];
            
            [imageV addSubview:inBtn];
        }
        
        [self.startPage addSubview:imageV];
    }
    
    // 设置scroll的滚动大小
    self.startPage.contentSize = CGSizeMake(_dataArray.count * self.view.frame.size.width, 0);
    self.startPage.showsHorizontalScrollIndicator = NO;
    self.startPage.showsVerticalScrollIndicator = NO;
    self.startPage.pagingEnabled = YES;
    self.startPage.bounces = NO;
    self.startPage.bouncesZoom = NO;
    self.startPage.delegate = self;
}

- (void)enterButtonAction {
    //设置不是第一次登陆
    NSNumber * isFirst = [NSNumber numberWithBool:1];
    [[NSUserDefaults standardUserDefaults] setValue:isFirst forKey:@"isFirst"];
    
    AppDelegate * appDelegate = [AppDelegate sharedLoginUserInfo];
    appDelegate.home = [HomePageViewController shareSingleton];
    
    TabBarViewController * rvc = [TabBarViewController new];
    appDelegate.home.midView = rvc;
    appDelegate.home.rightView = [RightPageViewController sharshareSingleViewController];
    appDelegate.window.rootViewController = appDelegate.home;
    [self presentViewController:appDelegate.home animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark scrollViewDelegate

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    _pgConl.currentPage = self.startPage.contentOffset.x / ScreenWidth;
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    _pgConl.currentPage = self.startPage.contentOffset.x / ScreenWidth;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
